package fonkin.com.wifisignapp.acticity.aboutMe;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.LoginActivity;
import fonkin.com.wifisignapp.bean.tissue.Department;
import fonkin.com.wifisignapp.bean.tissue.Job;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserInfor extends AppCompatActivity {

    EditText re_name,re_tel,re_email,re_age;
    Spinner re_dept,re_job;
    RadioGroup radioGroup;
    SharedPreferences pref;
    List<Department> departments = new ArrayList<>();//部门原数据源
    List<Department> deptList = new ArrayList<>();//部门封装后的数据源



    List<Job> jobList = new ArrayList<>();//职位封装后的数据源


    private ArrayAdapter<Department> deptAdapter;
    private ArrayAdapter<Job> jobAdapter;
    String deptId,jobId,deptName,jobName,sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_infor);

        initView();
    }

    private void initView() {



        pref = PreferenceManager.getDefaultSharedPreferences(UserInfor.this);
        re_name = (EditText) findViewById(R.id.et_name);//用户名
        if (pref.contains("user_name"))
        re_name.setText(pref.getString("user_name",""));

        re_tel = (EditText) findViewById(R.id.et_tel);//用户电话
        if (pref.contains("user_tel"))
        re_tel.setText(pref.getString("user_tel",""));

        re_email = (EditText) findViewById(R.id.et_email);//用户邮箱
        if (pref.contains("user_email"))
        re_email.setText(pref.getString("user_email",""));

        re_age = (EditText) findViewById(R.id.age);//用户年龄
        if (pref.contains("user_age"))
        re_age.setText(pref.getString("user_age",""));

        re_dept = (Spinner) findViewById(R.id.sp_dept);//所在学院
        re_job = (Spinner) findViewById(R.id.sp_job);//所在学院
        radioGroup = (RadioGroup)findViewById(R.id.modify_sex_check);//性别

        BaseRetrofit.GetBaseRetrofit().jobTreeList().enqueue(new Callback<ReturnVo<List<Department>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<Department>>> call, Response<ReturnVo<List<Department>>> response) {
                if (response.isSuccessful()&&response.body().getCode().equals(1)){
                    departments = response.body().getData();
                    for (Department depart:departments){
                        deptList.add(new Department(depart.getId(),depart.getDeptName()));
                    }
                    //实例ArrayAdapter对象。
                    deptAdapter = new ArrayAdapter<Department>(UserInfor.this,android.R.layout.simple_spinner_item,deptList);
                    deptAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    re_dept.setSelection(2,true);
                    //对Spinner设置数组适配器
                    re_dept.setAdapter(deptAdapter);
                    //对Spinner设置点击事件
                    re_dept.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            //获取选中的项的值
                            Department depart = deptList.get(position);
                            deptId = depart.getId();
                            deptName = depart.getDeptName();
                            for (Department dept:departments){
                                if (dept.getId().equals(depart.getId())){
                                    jobList = dept.getChildren();
                                }
                            }
                            initJobSpinner();
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                    re_dept.setVisibility(View.VISIBLE);
                }else{
                    ShowToast.ShowToastShort(AppConstants.Fail_REQUEST,UserInfor.this);
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<Department>>> call, Throwable t) {
                ShowToast.ShowToastShort(AppConstants.Fail_RESPONSE,UserInfor.this);
            }
        });
        /**
         * 性别选择操作
         */
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton viewById = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
                sex = viewById.getText().toString();
            }
        });

    }

    public void initJobSpinner(){
        jobAdapter = new ArrayAdapter<Job>(UserInfor.this,android.R.layout.simple_spinner_item,jobList);
        jobAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //re_job.setSelection(2,true);
        //对Spinner设置数组适配器
        re_job.setAdapter(jobAdapter);
        re_job.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Job job = jobList.get(position);
                jobId = job.getId();
                jobName = job.getJobPosition();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        re_job.setVisibility(View.VISIBLE);
    }

    public void update(View v){
        String name = re_name.getText().toString();
        String tel = re_tel.getText().toString();
        String email = re_email.getText().toString();
        String age = re_age.getText().toString();
        String userId = pref.getString("user_id","");
        Log.d("=======",name+"\n"+tel+"\n"+email+"\n"+age+"\n"+userId+"\n"+deptId+"\n"+deptName+sex+"\n"+jobId+"\n"+jobName);
        BaseRetrofit.GetBaseRetrofit().save(name,age,tel,email,deptId,deptName,jobId,jobName,sex,userId).enqueue(new Callback<ReturnVo<User>>() {
            @Override
            public void onResponse(Call<ReturnVo<User>> call, Response<ReturnVo<User>> response) {
                if (response.isSuccessful()){
                    if (response.body().getCode().equals(1)) {
                        ShowToast.CenterPicToast("修改成功",UserInfor.this,R.drawable.ic_check_circle_black_24dp );
                    }else{
                        ShowToast.CenterPicToast("修改失败",UserInfor.this,R.drawable.ic_clear_black_24dp);
                    }
                }else{
                    ShowToast.ShowToastShort(AppConstants.Fail_REQUEST,UserInfor.this);
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<User>> call, Throwable t) {
                ShowToast.ShowToastShort(AppConstants.Fail_RESPONSE,UserInfor.this);
            }
        });




    }



}
